namespace SimpleWebServices.Models
{
    using System;
    using System.Xml.XPath;
    using SimpleWebServices;
    using SimpleWebServices.Security.Cryptography;
    using SimpleWebServices.Web;
    using SimpleWebServices.Web.UI.Html;
    using SimpleWebServices.Web.UI.Html.Elements;
    using Xunit;

    public sealed class HtmlTemplateFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<HtmlTemplate>()
                .DerivesFrom<Resource>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("template")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            HtmlTemplate actual = (
                "<template>" +
                    "<div id='greeting'>" +
                        "<p>Hello</p>" +
                    "</div>" +
                "</template>").Deserialize<HtmlTemplate>();

            Assert.NotNull(actual);

            Assert.Equal<Name>("greeting", actual.Div.Id);
            Assert.Equal<string>("Hello", ((actual.Div.Elements[0] as Paragraph).Elements[0] as TextString).Value);
        }

        [Fact]
        public void deserializeEmpty()
        {
            HtmlTemplate actual = "<template />".Deserialize<HtmlTemplate>();

            Assert.NotNull(actual);

            Assert.Null(actual.Div);
        }

        [Fact]
        public void serialize()
        {
            HtmlTemplate obj = new HtmlTemplate
            {
                Div = new Division
                {
                    Class = "greeting",
                    Elements = 
                    {
                        new Paragraph
                        {
                            Elements =
                            {
                                new TextString("Hello")
                            }
                        }
                    }
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/template/div[@class='greeting']/p[text()='Hello'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new HtmlTemplate());
        }

        [Fact]
        public void prop_Div()
        {
            Assert.True(new PropertyExpectations(typeof(HtmlTemplate).GetProperty("Div"))
                .XmlElement("div")
                .IsAutoProperty<Division>()
                .Result);
        }

        [Fact]
        public void prop_Entity()
        {
            Assert.True(new PropertyExpectations(typeof(HtmlTemplate).GetProperty("Entity"))
                .XmlIgnore()
                .TypeIs<string>()
                .DefaultValueIsNotNull()
                .Result);
        }

        [Fact]
        public void prop_Entity_get()
        {
            HtmlTemplate obj = new HtmlTemplate
            {
                Div = new Division
                {
                    Class = "greeting"
                }
            };

            string expected = "UiamtuyZnAmW7Y+1Iy4tbw==";
            string actual = MD5Hash.Compute(obj.Entity);

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void prop_Entity_getEmpty()
        {
            string expected = string.Empty;
            string actual = new HtmlTemplate().Entity;

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void op_SetText_string_string()
        {
            HtmlTemplate obj = (
                "<template>" +
                    "<div>" +
                        "<p id='name'>abc</p>" +
                    "</div>" +
                "</template>").Deserialize<HtmlTemplate>();

            Assert.Equal<string>("abc", ((obj.Div["name"] as IInnerElements).Elements[0] as TextString).Value);

            obj.SetText("name", "xyz");

            Assert.Equal<string>("xyz", ((obj.Div["name"] as IInnerElements).Elements[0] as TextString).Value);
        }

        [Fact]
        public void op_SetText_string_string_whenEmpty()
        {
            HtmlTemplate obj = (
                "<template>" +
                    "<div>" +
                        "<p id='name'></p>" +
                    "</div>" +
                "</template>").Deserialize<HtmlTemplate>();

            obj.SetText("name", "xyz");

            Assert.Equal<string>("xyz", ((obj.Div["name"] as IInnerElements).Elements[0] as TextString).Value);
        }

        [Fact]
        public void op_SetText_stringNull_string()
        {
            Assert.Throws<ArgumentNullException>(() => new HtmlTemplate().SetText(null as string, "xyz"));
        }

        [Fact]
        public void op_SetText_stringEmpty_string()
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => new HtmlTemplate().SetText(string.Empty, "xyz"));
        }

        [Fact]
        public void op_SetText_string_stringNull()
        {
            HtmlTemplate obj = (
                "<template>" +
                    "<div>" +
                        "<p id='name'>abc</p>" +
                    "</div>" +
                "</template>").Deserialize<HtmlTemplate>();

            Assert.Equal<string>("abc", ((obj.Div["name"] as IInnerElements).Elements[0] as TextString).Value);

            obj.SetText("name", null);

            Assert.Equal<int>(0, (obj.Div["name"] as IInnerElements).Elements.Count);
        }

        [Fact]
        public void op_SetText_string_stringEmpty()
        {
            HtmlTemplate obj = (
                "<template>" +
                    "<div>" +
                        "<p id='name'>abc</p>" +
                    "</div>" +
                "</template>").Deserialize<HtmlTemplate>();

            Assert.Equal<string>("abc", ((obj.Div["name"] as IInnerElements).Elements[0] as TextString).Value);

            obj.SetText("name", string.Empty);

            Assert.Equal<string>(string.Empty, ((obj.Div["name"] as IInnerElements).Elements[0] as TextString).Value);
        }
    }
}